package ctrl;

import service.ICartService;
import service.Impl.ICartServiceImpl;
import service.Impl.IClientServiceImpl;
import service.Impl.IWishListServiceImpl;
import utils.WebUtils;
import vo.Client;
import vo.WishList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "SignUpServlet")
public class SignUpServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        IClientServiceImpl clientService = new IClientServiceImpl();
        ICartServiceImpl cartService=new ICartServiceImpl();
        IWishListServiceImpl wishListService = new IWishListServiceImpl();
        Client client = WebUtils.copyToBean(request, Client.class);
        if(clientService.checkPhoneNumber(client.getPhoneNumber())){
            response.getWriter().write(100);//100代表用户电话号码已注册过
        }else {
            clientService.addClient(client);

            //添加业务逻辑，注册用户时为用户生成一个自己的购物车（zx）
            Client client1=clientService.selectClient(client.getPhoneNumber());
            int userId=client1.getId();
            cartService.addCart(userId);
            //添加业务逻辑，注册用户时为用户生产一个愿望清单
            WishList wishList = new WishList();
            wishList.setUserId(userId);
            wishListService.addWishList(wishList);

        }
        response.setContentType("text/html;charset=utf-8");
        request.getRequestDispatcher("/login.jsp").forward(request, response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
